//
//  SpotEditInfoImageView.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/7/7.
//

import UIKit

class SpotEditInfoImageView: UIView,UICollectionViewDelegate, UICollectionViewDataSource {
    typealias SupportValueViewBlock = () -> Void
    var supportValueViewBlock : SupportValueViewBlock!
    typealias SupportClickImageBlock = (_ ary:Array<Any>) -> Void
    var supportClickImageBlock : SupportClickImageBlock!

    let countNum = 3
    var isTitle:Bool = false
    var isBig:Bool = false
    var VC:UIViewController!
    
    init(frame: CGRect,vc:UIViewController,isTitle:Bool,isBig:Bool) {
        super.init(frame: frame)
        self.VC = vc
        self.isTitle = isTitle
        self.isBig = isBig
        self.backgroundColor = UIColor.white
        self.setUi()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setUi() {
        if isTitle {
            self.addSubview(self.label)
            label.snp.makeConstraints { (make) in
                make.top.equalTo(self.snp.top).offset(5);
                make.left.equalTo(self.snp.left).offset(15);
                make.height.equalTo(15);
            }
        }
        self.addSubview(self.collectionView)
    }
    @objc func protectAction() {
        if (supportValueViewBlock != nil) {
            supportValueViewBlock()
        }
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.endEditing(true)
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataDicAry.count == countNum ? countNum : self.dataDicAry.count+1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.row != self.dataDicAry.count {
            let cell:SpotImgCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SpotImgCollectionCell
            cell.editImageView.isHidden = indexPath.row == self.dataDicAry.count ? true : false
            //zs阿里云待处理
                
            cell.clickImageViewCellBlock = {
                if (self.supportClickImageBlock != nil) {
                    self.supportClickImageBlock(self.dataDicAry)
                }
            }
            cell.clickEditCellBlock = {
                self.dataDicAry.remove(at: indexPath.row)
                NotificationCenter.default.post(Notification.init(name: Notification.Name(rawValue: "refreshImgCount")))
                self.collectionView.reloadData()
            }
            return cell
        } else {
            let cell:SpotImgPlaceholderCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! SpotImgPlaceholderCell
            cell.clickImageViewCellBlock = {
                //zs待处理图片展示
            }
            cell.textNumLab.text = String.init(format: "%ld", dataDicAry.count) + "/3"
            return cell
        }
        let cell:UICollectionViewCell = UICollectionViewCell()
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row != self.dataDicAry.count {
            //zs待处理查看大图
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if isBig {
            return CGSize.init(width: (Constants.screenWidth - Constants.ratioInterval * 60 ) / 2, height: (Constants.screenWidth - Constants.ratioInterval * 60 ) / 2)
        } else {
            return CGSize(width: Int(Constants.screenWidth) / 4, height: Int(Constants.screenWidth) / 4)
        }
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return Constants.ratioInterval * 25
    }
    
    lazy var collectionView: UICollectionView = {
        let collectionView:UICollectionView!
        if isBig {
            collectionView = UICollectionView.init(frame: CGRect.init(x: 20, y: isTitle ? 30 : 0, width: Constants.screenWidth - Constants.ratioInterval * 40, height: Constants.screenWidth), collectionViewLayout: self.flowLayout)
        } else {
            collectionView = UICollectionView.init(frame: CGRect.init(x: 20, y: isTitle ? 30 : 0, width: Constants.screenWidth / 4 * 3 - Constants.ratioInterval * 40, height: Constants.screenWidth), collectionViewLayout: self.flowLayout)
        }
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.white
        collectionView.register(SpotImgCollectionCell.self, forCellWithReuseIdentifier: "cell")
        collectionView.register(SpotImgPlaceholderCell.self, forCellWithReuseIdentifier: "cell2")
        collectionView.isScrollEnabled = false
        
        let addressTap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(protectAction))
        collectionView.isUserInteractionEnabled = true
        collectionView.addGestureRecognizer(addressTap)
        return collectionView
    }()
    lazy var flowLayout: UICollectionViewFlowLayout = {
        let flowLayout = UICollectionViewFlowLayout.init()
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.sectionInset = UIEdgeInsets.init(top: 8, left: 0, bottom: 8, right: 0)
        return flowLayout
    }()
    lazy var dataDicAry: Array<String> = {
        let dataDicAry = Array<String>()
        return dataDicAry
    }()
    
    lazy var label: UILabel = {
        let label = UILabel.init()
        label.font = UIFont.boldSystemFont(ofSize: 14)
        label.textColor = UIColor.init(hexString: "#333333")
        label.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_goods_photo)
        return label
    }()
}
